﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Results.Models
{
    /// <summary>
    /// Player details
    /// </summary>
    public class PlayerModel : IComparable
    {
        /// <summary>
        /// Data
        /// </summary>
        public String FirstName { get; set; }
        public String LastName { get; set; }
        public String Hometown { get; set; }
        public String Gender { get; set; }
        public String GraduationYear { get; set; }

        /// <summary>
        /// The scores of the tournaments the player participated in
        /// </summary>
        public List<ResultModel> Results { get; private set; }

        public PlayerModel()
        {
            FirstName = "";
            LastName = "";
            Results = new List<ResultModel>();
        }

        /// <summary>
        /// Did this player participate in any tournaments as a player in the specified group during the specified season?
        /// </summary>
        /// <param name="selectedGroup">e.g. "Men 14-16"</param>
        /// <param name="selectedSeason">e.g. "2010"</param>
        /// <returns></returns>
        public Boolean Contains(string selectedGroup, string selectedSeason)
        {
            return Results.Count(resultModel => resultModel.Group == selectedGroup && resultModel.TournamentModel.Season == selectedSeason) > 0;
        }

        #region IComparable Members

        /// <summary>
        /// Sort players by last name, first name
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int CompareTo(object obj)
        {
            var otherPlayerModel = obj as PlayerModel;
            if (otherPlayerModel == null)
            {
                return 0;
            }

            int retVal = LastName.CompareTo(otherPlayerModel.LastName);
            if (retVal != 0)
            {
                return retVal;
            }

            return FirstName.CompareTo(otherPlayerModel.FirstName);
        }

        #endregion
    }
}
